﻿using AutoMapper;
using MediatR;
using SC.GirdManage.Common;
using SC.GirdManage.Repository;
using SC.GridManage.API.Read.Application.command.UnitGrid;
using SC.GridManage.API.Read.Extensions;
using SC.GridManage.Domain.Models;
using System.Linq;

namespace SC.GridManage.API.Read.Application.handler.UnitGrid
{
    /// <summary>
    /// 中介者处理类 - 单元网格的列表查询
    /// </summary>
    public class GetUnitGirdHandler : IRequestHandler<GetUnitGridCommand, ApiResult<PageResult<GetUnitGridDTO>>>
    {
        private readonly IBaseRepository<UnitGridModel> unitGridRepo;
        private readonly IBaseRepository<AreasModel> areaRepo;
        private readonly IMapper mapper;

        public GetUnitGirdHandler(IBaseRepository<UnitGridModel> unitGridRepo, IBaseRepository<AreasModel> areaRepo, IMapper mapper)
        {
            this.unitGridRepo = unitGridRepo;
            this.areaRepo = areaRepo;
            this.mapper = mapper;
        }

        /// <summary>
        /// 通过GridName来查询单元网格的分页列表
        /// </summary>
        /// <param name="request">查询命令参数</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiResult<PageResult<GetUnitGridDTO>>> Handle(GetUnitGridCommand request, CancellationToken cancellationToken)
        {
            //通用响应包装类,用来包装分页数据和响应结果
            ApiResult<PageResult<GetUnitGridDTO>> ressult = new ApiResult<PageResult<GetUnitGridDTO>>();

            //所有未删除的单元网格
            var unitgrids = unitGridRepo.QueryAsync(x => x.IsDeleted == false).Result;
            //条件判断,如果GridName网格名称不为空,则加入这个过滤条件
            if (!request.GridName.IsNullOrEmpty())
            {
                unitgrids = unitgrids.Where(x => x.GridName.Contains(request.GridName));
            }

            var areas = areaRepo.QueryAsync().Result;//所有区域数据

            //两表联查的linq查询
            var linq = from u in unitgrids
                       join a in areas on u.AreaId equals a.Id
                       select new GetUnitGridDTO
                       {
                           Id = u.Id,
                           AreaName = a.AreaName,
                           AreaId = a.Id,
                           GridNo = u.GridNo,
                           GridName = u.GridName
                       };
            var count = linq.Count();//分页要用到的总条数
            //分页 先排序 再跳过 然后取指定页码的数据 最后ToList取数据
            var unitdto = linq.OrderByDescending(x => x.Id).Skip((request.Index - 1) * request.Size).Take(request.Size).ToList();

            //组装Page分页对象,返回分页总条数 分页总页数以及分页的列表集合数据
            PageResult<GetUnitGridDTO> page = new PageResult<GetUnitGridDTO>();
            page.TotalCount = count;
            page.TotalPage = (int)Math.Ceiling(count * 1.0 / request.Size);
            page.Data = unitdto;

            //把分页数据page赋值给ApiResult的对象
            ressult.Data = page; 
            ressult.Code = GirdManage.Common.Enums.EnumResultCode.Success;
            ressult.Msg = "查询单元网格分页成功!";

            return Task.FromResult(ressult);

        }
    }
}
